<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 15/06/11
 * Time: 18:11
 * To change this template use File | Settings | File Templates.
 */

class SQL_Result
{
    private $result;

    private $last_row;
    private $rowset;

    public function __construct($result)
    {
        if (!is_resource($result)) {
            throw new Exception('$result is not a valid resource');
        }
        $this->result = $result;
    }

    public function fetch()
    {
        $this->last_row = mysql_fetch_assoc($this->result);
        return $this->last_row;
    }

    public function next()
    {
        $this->fetch();
    }

    public function getRow()
    {
        return $this->last_row;
    }

    public function fetchField($field)
    {
        if (!is_array($this->last_row))
            $this->fetch();
        return $this->last_row[$field];
    }

    public function isValid()
    {
        return $this->current() != false;
    }

    public function current()
    {
        if (!is_array($this->last_row))
            $this->fetch();
        return $this->last_row;
    }

    public function seek($id)
    {
        if (!mysql_data_seek($this->result, $id))
            throw new Exception('invalid row');
    }
}
